package chuichui.game.render
{
	import chuichui.Utils.Print;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;


	public class RenderData
	{
		private static var renderIndex : int = 0;
		public var id : String;
		private var _x : int;
		private var _y : int;

		/**
		 * 渲染对象
		 */
		protected var bitmap : Bitmap;
		/**
		 * 子级
		 */
		protected var childList : Array;
		/**
		 * 父类
		 */
		protected var _parent : RenderData;

		/**
		 * 由大到小，数值越大，层级在最上层
		 */
		public var zIndex : int = 0;

		/**
		 * 是否锁定透明度值,可见度
		 */
		public var lock : Boolean = false;

		public function RenderData()
		{
			bitmap = new Bitmap();
			bitmap.name = String(renderIndex++);
			childList = [];
		}

		public function get x() : int
		{
			return _x;
		}

		public function set x(value : int) : void
		{
			_x = value;
			bitmap.x = _x;
		}

		public function get y() : int
		{
			return _y;
		}

		public function set y(value : int) : void
		{
			_y = value;
			bitmap.y = _y;
		}

		public function addChild(child : RenderData) : RenderData
		{
			if(childList.indexOf(child) != -1)
			{
				return null;
			}

			if(child == this)
			{
				Print.error("不能添加自己为子级", this);
				return null;
			}

			if(child.parent)
				child.parent.removeChild(child);
			childList.push(child);
			child.parent = this;
			childList.sortOn("zIndex", Array.NUMERIC);
			//RenderManager.getInstance().checkAddRender(this);
			return child;
		}

		public function removeChild(child : RenderData) : RenderData
		{
			var index : int = childList.indexOf(child);
			return removeChildAt(index);
		}

		public function removeChildAt(index : int) : RenderData
		{
			if(index < 0 || index >= numChildren)
				return null;
			var child : RenderData = childList[index];
			child.parent = null;
			childList.splice(index, 1);
			return child;
		}

		public function getChildAt(index : int) : RenderData
		{
			if(index < 0 || index >= numChildren)
				return null;
			return childList[index];
		}

		public function getChildIndex(child : RenderData) : int
		{
			return childList.indexOf(child);
		}

		public function getChildByName(name : String) : RenderData
		{
			for(var i : int = numChildren - 1; i >= 0; i--)
			{
				var child : RenderData = getChildAt(i);

				if(child.name == name)
					return child;
			}
			return null;
		}

		public function removeAllChildren() : void
		{
			var child : RenderData;

			for(var i : int = numChildren - 1; i >= 0; i--)
			{
				child = getChildAt(i);
				child.destory();
			}
			childList.length = 0;
		}

		/**
		 * 子类的数量
		 * @return
		 *
		 */
		public function get numChildren() : int
		{
			return childList.length;
		}

		public function set parent(value : RenderData) : void
		{
			_parent = value;

			if(_parent)
			{
				alpha = _parent.alpha;
				visible = _parent.visible;
				colorTransform = _parent.colorTransform;
			}
		}

		public function get parent() : RenderData
		{
			return _parent;
		}

		/**
		 * 是否可见
		 * @return
		 *
		 */
		public function get visible() : Boolean
		{
			return bitmap.visible;
		}

		public function set visible(value : Boolean) : void
		{
			if(lock || bitmap.visible == value)
			{
				return;
			}
			bitmap.visible = value;

			for each(var child : RenderData in childList)
			{
				child.visible = value;
			}
		}

		/**
		 * 颜色滤镜
		 * @return
		 *
		 */
		public function get colorTransform() : ColorTransform
		{
			return bitmap.transform.colorTransform;
		}

		public function set colorTransform(value : ColorTransform) : void
		{
			if(lock || value == null)
			{
				return;
			}

			if(bitmap.transform.colorTransform != value)
			{
				bitmap.transform.colorTransform = value;
			}

			for each(var child : RenderData in childList)
			{
				child.colorTransform = value;
			}
		}

		/**
		 * 透明度
		 * @param value
		 *
		 */
		public function set alpha(value : Number) : void
		{
			if(lock || bitmap.alpha == value)
			{
				return;
			}
			bitmap.alpha = value;

			for each(var child : RenderData in childList)
				child.alpha = value;
		}

		public function get alpha() : Number
		{
			return bitmap.alpha;
		}

		public function get name() : String
		{
			return bitmap.name;
		}

		public function set name(value : String) : void
		{
			bitmap.name = value;
		}


		public function get blendMode() : String
		{
			return bitmap.blendMode;
		}

		public function set blendMode(value : String) : void
		{
			value = value == '' ? null : value;

			if(bitmap.blendMode != value)
			{
				bitmap.blendMode = value;
			}

			for each(var child : RenderData in childList)
				child.blendMode = value;
		}

		public function set width(value : Number) : void
		{
			if(bitmap.width == value)
				return;
			bitmap.width = value;

			for each(var child : RenderData in childList)
				child.width = value;
		}

		public function get width() : Number
		{
			return bitmap.width;
		}

		public function set height(value : Number) : void
		{
			if(bitmap.height == value)
				return;
			bitmap.height = value;

			for each(var child : RenderData in childList)
				child.height = value;
		}

		public function get height() : Number
		{
			return bitmap.height;
		}

		public function get scaleX() : Number
		{
			return bitmap.scaleX;
		}

		public function set scaleX(value : Number) : void
		{
			if(bitmap.scaleX == value)
			{
				return;
			}
			bitmap.scaleX = value;
		}

		public function get scaleY() : Number
		{
			return bitmap.scaleY;
		}

		public function set scaleY(value : Number) : void
		{
			if(bitmap.scaleY == value)
			{
				return;
			}
			bitmap.scaleY = value;
		}

		public function set bitmapData(value : BitmapData) : void
		{
			if(bitmap.bitmapData == value)
			{
				return;
			}
			bitmap.bitmapData = value;
		}

		public function get bitmapData() : BitmapData
		{
			return bitmap.bitmapData;
		}

		public function set filters(value : Array) : void
		{
			if(bitmap == null || bitmap.filters == value)
				return;
			bitmap.filters = value;

			for each(var child : RenderData in childList)
			{
				child.filters = value;
			}
		}

		public function get filters() : Array
		{
			return bitmap.filters;
		}

		public function get render() : DisplayObject
		{
			return bitmap;
		}

		public function addShow() : void
		{
			//RenderManager.getInstance().addRender(this);
		}

		public function removeShow() : void
		{
			//RenderManager.getInstance().removeRender(this);
		}

		private function resetBitmap() : void
		{
			if(bitmap)
			{
				bitmap.alpha = 1;
				bitmap.scaleX = 1;
				bitmap.scaleY = 1;
				bitmap.filters = [];
				bitmap.visible = true;
				bitmap.bitmapData = null;

				if(bitmap.parent)
				{
					bitmap.parent.removeChild(bitmap);
				}
			}
			removeAllChildren();
		}

		public function destory() : void
		{
			lock = false;
			_parent = null;
			removeShow();
			resetBitmap();
		}
	}
}